-module(heathrow).
-compile([export_all]).

calculate() ->
    Map = [{{a,50},{b,30},{c,10}},
           {{d,5}, {e,20},{f,90}},
           {{g,40},{h,25},{i,2}},
           {{j,10},{k,0}, {l,8}}
          ],
    calculate(Map,{0,[]},{0,[]}).

calculate([],{DA,PA},{DB,PB}) ->
    if DA < DB -> {lists:reverse(PA),DA};
       true    -> {lists:reverse(PB),DB}
    end;
calculate([{{E1,X},{E2,Y},{E3,Z}}|Rs],{DA,PA},{DB,PB}) ->
    A1 = DA + X,
    A2 = DB + Y + Z,
    B1 = DA + X + Y,
    B2 = DB + Z,

    calculate(Rs,
              erlang:min({A1,[E1|PA]},{A2,[E2,E3|PB]}),
              erlang:min({B1,[E2,E1|PA]}, {B2,[E3|PB]})).

